/*
 * FileChoosePanel.java
 *
 * Created on 30 Март 2008 г., 13:53
 */
package pm.gui.components.basepanels;

import pm.devices.drives.Drive;
import pm.gui.components.DeviceLabel;

import javax.swing.*;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/** @author Администратор */
public class FileChoosePanel extends javax.swing.JPanel {

    /** Creates new form FileChoosePanel */
    public FileChoosePanel() {
        initComponents();
    }
    private DeviceComboBoxModel deviceComboBoxModel;
    private FileListModel fileListModel;

    public void setDevices(Drive[] drives) {
        deviceComboBoxModel.setDevices(drives);
    }

    public void changeDir(String dir) {
        fileListModel.changeDir(dir);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        openMethodButtonGroup = new javax.swing.ButtonGroup();
        deviceComboBoxLabel = new javax.swing.JLabel();
        driveComboBox = new javax.swing.JComboBox();
        openFolderButton = new javax.swing.JButton();
        parentDirButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        textRadioButton = new javax.swing.JRadioButton();
        pdfRadioButton = new javax.swing.JRadioButton();
        imageRadioButton = new javax.swing.JRadioButton();
        openButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        driveNotSelectedLabel = new javax.swing.JLabel();
        fileChooseScrollPane = new javax.swing.JScrollPane();
        filesList = new javax.swing.JList();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("pm/gui/components/basepanels/Bundle"); // NOI18N
        deviceComboBoxLabel.setText(bundle.getString("FileChoosePanel.deviceComboBoxLabel.text")); // NOI18N

        deviceComboBoxModel = new DeviceComboBoxModel();
        driveComboBox.setModel(deviceComboBoxModel);

        openFolderButton.setText(bundle.getString("FileChoosePanel.openFolderButton.text_1")); // NOI18N
        openFolderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openFolderButtonActionPerformed(evt);
            }
        });

        parentDirButton.setText(bundle.getString("FileChoosePanel.parentDirButton.text")); // NOI18N
        parentDirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parentDirButtonActionPerformed(evt);
            }
        });

        backButton.setText(bundle.getString("FileChoosePanel.backButton.text")); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        openMethodButtonGroup.add(textRadioButton);
        textRadioButton.setSelected(true);
        textRadioButton.setText(bundle.getString("FileChoosePanel.textRadioButton.text")); // NOI18N

        openMethodButtonGroup.add(pdfRadioButton);
        pdfRadioButton.setText(bundle.getString("FileChoosePanel.pdfRadioButton.text")); // NOI18N

        openMethodButtonGroup.add(imageRadioButton);
        imageRadioButton.setText(bundle.getString("FileChoosePanel.imageRadioButton.text")); // NOI18N

        openButton.setText(bundle.getString("FileChoosePanel.openButton.text")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(pdfRadioButton)
                        .addGap(67, 67, 67)
                        .addComponent(openButton))
                    .addComponent(textRadioButton)
                    .addComponent(imageRadioButton))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(textRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(openButton)
                    .addComponent(pdfRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(imageRadioButton))
        );

        jPanel2.setLayout(new java.awt.BorderLayout());

        driveNotSelectedLabel.setText(bundle.getString("FileChoosePanel.deviceNotSelectedLabel.text")); // NOI18N
        jPanel2.add(driveNotSelectedLabel, java.awt.BorderLayout.NORTH);

        fileChooseScrollPane.setWheelScrollingEnabled(false);
        fileChooseScrollPane.setVisible(false);

        fileListModel = new FileListModel();
        filesList.setModel(fileListModel);
        filesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        filesList.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
        fileChooseScrollPane.setViewportView(filesList);

        jPanel2.add(fileChooseScrollPane, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(deviceComboBoxLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(driveComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(parentDirButton)
                            .addComponent(openFolderButton)
                            .addComponent(backButton)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(56, 56, 56))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {backButton, openFolderButton, parentDirButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(deviceComboBoxLabel)
                    .addComponent(driveComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(openFolderButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(parentDirButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(backButton)
                        .addGap(50, 50, 50)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
                .addGap(150, 150, 150))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void parentDirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parentDirButtonActionPerformed
            File currentDir = (File) fileListModel.getCurrentDir();
            System.out.println(currentDir.getPath());
            if (currentDir.getParentFile() != null) {
                System.out.println(currentDir.getParent());
                changeDir(currentDir.getParent());
            }
}//GEN-LAST:event_parentDirButtonActionPerformed

        private void openFolderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openFolderButtonActionPerformed
            File selectedDir = (File) filesList.getSelectedValue();
            if (selectedDir.isDirectory()) {
                changeDir(selectedDir.getPath());
            }
}//GEN-LAST:event_openFolderButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton backButton;
    public javax.swing.JComboBox driveComboBox;
    public javax.swing.JLabel deviceComboBoxLabel;
    public javax.swing.JLabel driveNotSelectedLabel;
    public javax.swing.JScrollPane fileChooseScrollPane;
    public javax.swing.JList filesList;
    public javax.swing.JRadioButton imageRadioButton;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JPanel jPanel2;
    public javax.swing.JButton openButton;
    public javax.swing.JButton openFolderButton;
    public javax.swing.ButtonGroup openMethodButtonGroup;
    public javax.swing.JButton parentDirButton;
    public javax.swing.JRadioButton pdfRadioButton;
    public javax.swing.JRadioButton textRadioButton;
    // End of variables declaration//GEN-END:variables
    private class FileListModel implements ListModel {

        private File currentDir;

        public File getCurrentDir() {
            return currentDir;
        }
        private File[] childFiles;
        private List<ListDataListener> listeners;
        private JList source;

        FileListModel() {
            listeners = new ArrayList<ListDataListener>();
            source = filesList;
        }

        void changeDir(String dir) {
            currentDir = new File(dir);
            childFiles = currentDir.listFiles();
            Arrays.sort(childFiles, new Comparator<File>() {

                public int compare(File o1, File o2) {
                    //o1 less then o2, then -1
                    if (o1.isDirectory() ^ o2.isDirectory()) {
                        return o1.isDirectory() ? -1 : 1;
                    }
                    return 0;
                }
            });
            for (ListDataListener listener : listeners) {
                listener.contentsChanged(new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, 0, childFiles.length));
            }
        }

        public int getSize() {
            if (childFiles == null) {
                return 0;
            }
            return childFiles.length;
        }

        public Object getElementAt(int index) {
            if (childFiles == null) {
                return null;
            }
            return childFiles[index];
        }

        public void addListDataListener(ListDataListener l) {
            listeners.add(l);
        }

        public void removeListDataListener(ListDataListener l) {
            listeners.remove(l);
        }
    }

    private class DeviceComboBoxModel implements ComboBoxModel {

        private Drive[] drives;
        private Drive selected;
        private JComboBox source;
        private List<ListDataListener> listeners;

        DeviceComboBoxModel() {
            source = driveComboBox;
            listeners = new ArrayList<ListDataListener>();
            source.setRenderer(new DeviceLabel());
        }

        public void setDevices(Drive[] drives) {
            this.drives = drives;
            if (source != null) {
                for (ListDataListener listener : listeners) {
                    listener.contentsChanged(
                            new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, 0, drives.length));
                }
            }
        }

        public void setSelectedItem(Object anItem) {
            selected = (Drive) anItem;
        }

        public Object getSelectedItem() {
            return selected;
        }

        public int getSize() {
            if (drives == null) {
                return 0;
            }
            return drives.length;
        }

        public Object getElementAt(int index) {
            if (drives == null) {
                return null;
            }
            return drives[index];
        }

        public void addListDataListener(ListDataListener l) {
            listeners.add(l);
        }

        public void removeListDataListener(ListDataListener l) {
            listeners.remove(l);
        }
    }
}
